// 敏感操作日志相关API接口
import axios from 'axios'

// 创建axios实例
const api = axios.create({
  baseURL: '/api', // 后端API基础路径
  timeout: 10000,
  withCredentials: true // 支持session
})

// 请求拦截器
api.interceptors.request.use(
  config => {
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    console.error('API请求错误:', error)
    // 如果后端返回了错误响应，提取错误信息
    if (error.response && error.response.data) {
      return Promise.reject(error.response.data)
    }
    return Promise.reject(error)
  }
)

// 敏感操作日志接口类型定义
export interface SensitiveLog {
  id: number
  operationDesc: string
  operationType: string
  operatorId: number
  operatorUsername: string
  operationTime: string
  createdAt: string
}

export interface SensitiveLogListResponse {
  list: SensitiveLog[]
  total: number
  pageNum: number
  pageSize: number
  pages: number
}

export interface SensitiveLogQuery {
  pageNum?: number
  pageSize?: number
  operationType?: string
  operatorUsername?: string
  startTime?: string
  endTime?: string
}

export interface SensitiveLogStatistics {
  todayCount: number
  weekCount: number
  monthCount: number
  totalCount: number
}

export interface ApiResponse<T = any> {
  code: number
  msg: string
  data: T
}

// 敏感操作日志API接口
export const sensitiveLogApi = {
  // 分页查询敏感操作日志
  getSensitiveLogs(query: SensitiveLogQuery = {}): Promise<ApiResponse<SensitiveLogListResponse>> {
    return api.get('/admin/sensitive-logs', { params: query })
  },

  // 查询敏感操作日志详情
  getSensitiveLogDetail(id: number): Promise<ApiResponse<SensitiveLog>> {
    return api.get(`/admin/sensitive-logs/${id}`)
  },

  // 获取敏感操作日志统计
  getSensitiveLogStatistics(): Promise<ApiResponse<SensitiveLogStatistics>> {
    return api.get('/admin/sensitive-logs/statistics')
  },

  // 清理敏感操作日志
  cleanSensitiveLogs(days: number = 30): Promise<ApiResponse<string>> {
    return api.delete('/admin/sensitive-logs/clean', { params: { days } })
  }
} 